﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using RegExStudio.RegExStudioEventArgs;

namespace RegExStudio.UserControls
{
    /// <summary>
    /// Zeigt/Ändert die RegExOptions
    /// </summary>
    public partial class CtrlOptions : CtrlBase
    {

        private RegexOptions _options = RegexOptions.None;
        /// <summary>
        /// Die RegExOptions die gewählt sind
        /// Im Setter werden die zu den Optionen gehörenden CheckBoxen un/gecheckt
        /// </summary>
        public RegexOptions Options
        {
            get { return _options; }
            set 
            { 
                _options = value;

                if (_options.HasFlag(RegexOptions.CultureInvariant))
                {
                    chkCultureInvariant.Checked = true;
                }
                else
                {
                    chkCultureInvariant.Checked = false;
                }

                if (_options.HasFlag(RegexOptions.ECMAScript))
                {
                    chkECMAScript.Checked = true;
                }
                else
                {
                    chkECMAScript.Checked = false;
                }

                if (_options.HasFlag(RegexOptions.ExplicitCapture))
                {
                    chkExplicitCapture.Checked = true;
                }
                else
                {
                    chkExplicitCapture.Checked = false;
                }

                if (_options.HasFlag(RegexOptions.IgnoreCase))
                {
                    chkIgnoreCase.Checked = true;
                }
                else
                {
                    chkIgnoreCase.Checked = false;
                }

                if (_options.HasFlag(RegexOptions.IgnorePatternWhitespace))
                {
                    chkIgnorePatternWhitespace.Checked = true;
                }
                else
                {
                    chkIgnorePatternWhitespace.Checked = false;
                }

                if (_options.HasFlag(RegexOptions.Multiline))
                {
                    chkMultiline.Checked = true;
                }
                else
                {
                    chkMultiline.Checked = false;
                }

                if (_options.HasFlag(RegexOptions.RightToLeft))
                {
                    chkRightToLeft.Checked = true;
                }
                else
                {
                    chkRightToLeft.Checked = false;
                }

                if (_options.HasFlag(RegexOptions.Singleline))
                {
                    chkSingleline.Checked = true;
                }
                else
                {
                    chkSingleline.Checked = false;
                }

                OnOptionsChanged(_options);
            }
        }

        /// <summary>
        /// ctor
        /// </summary>
        public CtrlOptions()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Control wird geladen
        /// Eingabecontrol-Hintergrundfärbung aktivieren
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CtrlOptions_Load(object sender, EventArgs e)
        {
            chkCultureInvariant.Enter += new EventHandler(Control_Enter);
            chkCultureInvariant.Leave += new EventHandler(Control_Leave);

            chkECMAScript.Enter += new EventHandler(Control_Enter);
            chkECMAScript.Leave += new EventHandler(Control_Leave);

            chkExplicitCapture.Enter += new EventHandler(Control_Enter);
            chkExplicitCapture.Leave += new EventHandler(Control_Leave);

            chkIgnoreCase.Enter += new EventHandler(Control_Enter);
            chkIgnoreCase.Leave += new EventHandler(Control_Leave);

            chkIgnorePatternWhitespace.Enter += new EventHandler(Control_Enter);
            chkIgnorePatternWhitespace.Leave += new EventHandler(Control_Leave);

            chkMultiline.Enter += new EventHandler(Control_Enter);
            chkMultiline.Leave += new EventHandler(Control_Leave);

            chkRightToLeft.Enter += new EventHandler(Control_Enter);
            chkRightToLeft.Leave += new EventHandler(Control_Leave);

            chkSingleline.Enter += new EventHandler(Control_Enter);
            chkSingleline.Leave += new EventHandler(Control_Leave);
        }

        /// <summary>
        /// Eine Option wurde geändert und merkt sich die gewählten Optionen
        /// </summary>
        /// <param name="sender">Die Checkbox der Option die geclickt wurde</param>
        /// <param name="e"></param>
        private void Options_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chkBox = sender as CheckBox;

            RegexOptions checkedOption = (RegexOptions)Enum.Parse(typeof(RegexOptions), chkBox.Tag.ToString());

            if (chkBox.Checked)
            {
                _options |= checkedOption;
            }
            else
            {
                _options -= checkedOption;
            }

            OnOptionsChanged(_options);
        }

        /// <summary>
        /// EventHandler für Änderung der RegExOptions
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public delegate void OptionsChangedEventHandler(object sender, OptionsChangedEventArgs e);

        /// <summary>
        /// Event das ausgelöst wird, wenn die RegExOptions geändert wurden
        /// </summary>
        public event OptionsChangedEventHandler OptionsChanged;

        /// <summary>
        /// Feuert das Event, wenn die RegExOptions geändert wurden
        /// </summary>
        /// <param name="options"></param>
        protected virtual void OnOptionsChanged(RegexOptions options)
        {
            if (OptionsChanged != null)
            {
                OptionsChanged(this, new OptionsChangedEventArgs(options));
            }
        }
    }
}
